package br.com.lh.urabuilder.command
{
	import br.com.lh.urabuilder.business.ViewManager;
	import br.com.lh.urabuilder.command.base.UraBaseCommand;
	import br.com.lh.urabuilder.domain.view.UraElement;
	import br.com.lh.urabuilder.event.SetInitialActionEvent;
	import br.com.lh.urabuilder.model.UraModelLocator;
	import br.com.lh.urabuilder.view.ActionComponent;
	
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;

	public class DeleteSelectedActionsCommand extends UraBaseCommand
	{
		private const modelLocator:UraModelLocator = UraModelLocator.getInstance();
		
		public function DeleteSelectedActionsCommand(){
		}

		public override function redo(event:CairngormEvent = null):void{
			var viewManager:ViewManager = modelLocator.viewManager;
			var selectedElements:ArrayCollection = viewManager.getSelectedComponents();
			for each (var element:UraElement in selectedElements){
				if (element== null){
					continue;
				}
				viewManager.removeComponent(element);
				if (element is ActionComponent){
					modelLocator.xmlManager.removeElement(ActionComponent(element).uraAction);
				}	
			}
			updateInitialActionCombo();
		}
		
		private function updateInitialActionCombo():void{
			modelLocator.selectableComponents.updateSelectableList();
			if (modelLocator.uraPropertiesForm ==null){
				return;
			}
			modelLocator.uraPropertiesForm.updatePanel();
			if (modelLocator.uraPropertiesForm.initialItem != null){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SetInitialActionEvent(modelLocator.uraPropertiesForm.initialItem.data));
			}else{
				CairngormEventDispatcher.getInstance().dispatchEvent(new SetInitialActionEvent(-1));
			}
			
		}
		
	}
}
